﻿using AutoMapper;
using Decoration.Api.Comm;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProjectInfoController : ControllerBase
    {
        private readonly IProjectInfoService svc;
        private readonly IOrderInfoService orderSvc;
        private readonly IMapper mapper;

        public ProjectInfoController(IProjectInfoService svc,IOrderInfoService orderSvc,IMapper mapper)
        {
            this.svc = svc;
            this.orderSvc = orderSvc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("getAll")]
        public Result Get()
        {
            var data = svc.Query(o => o.Status == 1);

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<ProjectDto> Get(string projectName = "", int pageSize = 10, int pageIndex = 1)
        {
            int total = 0;
            var data = svc.Query(projectName, pageSize, pageIndex, out total);

            return new PagedResult<ProjectDto>() { code = 1, msg = "成功", total = total, data = data };
        }

        /// <summary>
        /// 历史订单
        /// </summary>
        /// <param name="Customer"></param>
        /// <returns></returns>
        [HttpGet("myhistory")]
        public PagedResult<ProjectDto> MyHistory(int? Customer = null)
        {
            string sql = @" select	pj.*,zt.DictName as 'CompletedStatusName',o.OrderPict,business.BusinessName
								from ProjectInfos pj inner join vw_dict zt 
						 on pj.CompletedStatus=zt.DictValue and zt.DictTypeName = '项目完成状态'
						 inner join OrderInfos  o on o.Id=pj.OrderId
						  inner join BusinessInfos business on o.BusinessId = business.Id
						 where pj.Customer=@Customer and pj.CompletedStatus=2";

            string countSql = $"select count(*) from ({sql})";

            var data = svc.Query<ProjectDto>(sql, new { Customer = Customer });

            return new PagedResult<ProjectDto>() { code = 1, msg = "成功",data = data };
        }

        /// <summary>
        /// 获取我的项目
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet("getMy")]
        [MyAuthorize]
        public PagedResult<ProjectDto> GetMy(string projectName = "", int pageSize = 10, int pageIndex = 1)
        {
            var auth = RouteData.Values["auth"] as AuthInfo;
            

            int total = 0;
            var data = svc.Query(projectName, pageSize, pageIndex, out total,leader:auth!.UserId);

            return new PagedResult<ProjectDto>() { code = 1, msg = "成功", total = total, data = data };
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 项目添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(ProjectPostDto dto)
        {
            // 获取订单
            var order = orderSvc.Get(o=>o.Id == dto.OrderId);

            // 创建项目
            var project = new ProjectInfo()
            {
                OrderId = dto.OrderId,
                BusinessType = order.BusinessId,
                Period = dto.Period,
                ProjectName = dto.ProjectName,
                Leader = dto.Leader,
                LeaderName = dto.LeaderName,
                Customer = order.Customer,
                CustomerName = order.CustomerName,
                CompletedStatus = 0,
                CreateTime = DateTime.Now,
                Status = 1
            };

            order.ProjectStatus = 1;
            orderSvc.Update(order);

            if (svc.Add(project))
                return new Result() { code = 1, msg = "添加成功" };
            else
                return new Result() { code = -1, msg = "添加失败" };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id, BusinessPostDto dto)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            var entity = mapper.Map(dto, data);

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            data.Status = 0;

            if (svc.Update(data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }
    }
}
